<?php

namespace App\Command\Make;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\NullOutput;

#[Command]
class MakeCsmCommand extends HyperfCommand
{
    public function __construct()
    {
        parent::__construct('make:csm');
        $this->setDescription('Create a new controller class、 service class、 model class');
    }

    public function handle()
    {
        $name = $this->input->getArgument('name');
        $this->line('name: ' . $name);

        $params = ["command" => 'make:model', "name" => $name . 'Model', "-f" => true,];
        $input = new ArrayInput($params);
        $output = new NullOutput();

        /** @var \Psr\Container\ContainerInterface $container */
        $container = \Hyperf\Utils\ApplicationContext::getContainer();

        /** @var \Symfony\Component\Console\Application $application */
        $application = $container->get(\Hyperf\Contract\ApplicationInterface::class);
        $application->setAutoExit(false);
        // 这种方式: 不会暴露出命令执行中的异常, 不会阻止程序返回
        $exitCode = $application->run($input, $output);
        $this->line('-------------------' . $exitCode);

        $params = ["command" => 'make:service', "name" => $name . 'Service', "-f" => true, '--model' => $name . 'Model'];
        $input = new ArrayInput($params);
        $exitCode = $application->run($input, $output);
        $this->line('-------------------' . $exitCode);

        $params = ["command" => 'make:controller', "name" => $name . 'Controller', "-f" => true, '--service' => $name . 'Service'];
        $input = new ArrayInput($params);
        $exitCode = $application->run($input, $output);
        $this->line('-------------------' . $exitCode);

    }

    protected function getArguments()
    {
        return [
            ['name', InputArgument::OPTIONAL, 'csm name.']
        ];
    }
}